using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PolyDecomp
{

   public class Triangle
    {

      public Vector2 v1, v2, v3;

        //public Triangle(float x1, float y1, float x2, float y2, float x3, float y3):this()
        //{
           
        //    float dx1 = x2 - x1;
        //    float dx2 = x3 - x1;
        //    float dy1 = y2 - y1;
        //    float dy2 = y3 - y1;
        //    float cross = dx1 * dy2 - dx2 * dy1;
        //    bool ccw = (cross > 0);
        //    if (ccw)
        //    {
        //        x[0] = x1; x[1] = x2; x[2] = x3;
        //        y[0] = y1; y[1] = y2; y[2] = y3;
        //    }
        //    else
        //    {
        //        x[0] = x1; x[1] = x3; x[2] = x2;
        //        y[0] = y1; y[1] = y3; y[2] = y2;
        //    }
        //}

        public Triangle(Vector2 p1,Vector2 p2,Vector2 p3)
        {

            float dx1 = p2.X - p1.X;
            float dx2 = p3.Y - p1.X;
            float dy1 = p2.Y - p1.Y;
            float dy2 = p3.Y - p1.Y;
            float cross = dx1 * dy2 - dx2 * dy1;
            bool ccw = (cross > 0);
            if (ccw)
            {
                v1 = p1;
                v2 = p2;
                v3 = p3;
            }
            else
            {

                v1 = p1;
                v2 = p3;
                v3 = p2;
            }
        }

        //public void draw(SpriteBatch sb)
        //{
        //    draw( sb);
        //}

        public void draw(SpriteBatch sb)
        {
            //fill(fll);
            ////    stroke(strk);
            //noStroke();
            //if (isInside(mouseX, mouseY)) fill(0, 255, 0);
            //triangle(x[0], y[0], x[1], y[1], x[2], y[2]);


            var gd = sb.GraphicsDevice;
            BasicEffect effect = new BasicEffect(gd);

            effect.Projection = Matrix.CreateOrthographicOffCenter
                (0, gd.Viewport.Width,
                gd.Viewport.Height, 0,
                0, 1);
            effect.VertexColorEnabled = true;
            // triangles that are ccw.  d3d has opposite winding order (cw) than openGL.

            //r.GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexDeclaration);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                VertexPositionColor[] tris = new VertexPositionColor[3];

                // d3d wants clockwise.
                tris[0] = new VertexPositionColor(new Vector3(v1.X, v1.Y, 0), Color.Black);
                tris[1] = new VertexPositionColor(new Vector3(v2.X, v2.Y, 0), Color.GreenYellow);
                tris[2] = new VertexPositionColor(new Vector3(v3.X, v3.Y, 0), Color.Red);

                sb.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, tris, 0, 1);

            }
        }

        public bool isInside(float _x, float _y)
        {
            float vx2 = _x - v1.X; float vy2 = _y - v1.Y;
            float vx1 = v2.X - v1.X; float vy1 = v2.Y - v1.Y;
            float vx0 = v3.X - v1.X; float vy0 = v3.Y - v1.Y;

            float dot00 = vx0 * vx0 + vy0 * vy0;
            float dot01 = vx0 * vx1 + vy0 * vy1;
            float dot02 = vx0 * vx2 + vy0 * vy2;
            float dot11 = vx1 * vx1 + vy1 * vy1;
            float dot12 = vx1 * vx2 + vy1 * vy2;
            float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
            float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
            float v = (dot00 * dot12 - dot01 * dot02) * invDenom;

            return ((u > 0) && (v > 0) && (u + v < 1));
        }

    }
}